﻿package com.apts.jfinalbase;

import java.io.File;

import com.apts.constant.AppBaseConstant;
import com.apts.constant.AppConstant;
import com.apts.interceptor.GlobalAuthInterceptor;
import com.apts.interceptor.GlobalExceptionInterceptor;
import com.apts.mapper.DbMapperConfig;
import com.apts.routes.EnterpriseRoutes;
import com.apts.routes.IndexRoutes;
import com.apts.routes.ProductRoutes;
import com.apts.routes.QRcodeRoutes;
import com.apts.routes.Routes_PlanB_;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;

/**
 * JFinal基本配置
 * 
 * @author lwh @2016-03-16
 */
public class BaseConfig extends JFinalConfig {

	/**
	 * 常量配置
	 */
	@Override
	public void configConstant(Constants me) {
		// 常量初始化
		AppBaseConstant.initConstant("config.properties");
		// me.setViewType(ViewType.FREE_MARKER);
		String rootPath = JFinal.me().getServletContext().getRealPath("/");
		me.setBaseUploadPath( "upload" ); // 默认上传文件路径(相对根目录)
		me.setDevMode(AppBaseConstant.getValue(AppConstant.Dev_DevMode, Boolean.class));
	}

	/**
	 * 路由配置
	 */
	@Override
	public void configRoute(Routes me) {
		me.add(new IndexRoutes()); // 未登录界面路由
		me.add(new EnterpriseRoutes()); // 企业信息管理路由
		me.add(new ProductRoutes()); // 产品类型管理路由
		me.add(new QRcodeRoutes());// 增加二维码控制器的路由
		/**
		 * 全局备用路由
		 */
		me.add(new Routes_PlanB_());
	}

	/**
	 * 插件配置
	 */
	@Override
	public void configPlugin(Plugins me) {
		// 作业调度配置
		// QuartzPlugin quartzPlugin = new QuartzPlugin("job.properties");
		// me.add(quartzPlugin);
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(AppBaseConstant.getValue(AppConstant.Jdbc_Url, String.class),
				AppBaseConstant.getValue(AppConstant.Jdbc_User, String.class),
				AppBaseConstant.getValue(AppConstant.Jdbc_Password, String.class));
		me.add(c3p0Plugin);

		// 在应用中使用 ActiveRecord 插件非常方便地操作数据库
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);

		// 配置数据表映射
		DbMapperConfig.mapper(arp);

	}

	/**
	 * AOP配置
	 */
	@Override
	public void configInterceptor(Interceptors me) {
		// 全局异常拦截器
		me.add(new GlobalExceptionInterceptor());
		// 全局权限拦截器
		me.add(new GlobalAuthInterceptor());
	}

	/**
	 * 处理器配置
	 */
	@Override
	public void configHandler(Handlers me) {

	}

	/**
	 * mian方法启动项目
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}

}
